package com.wrupple.vegetate.server.integration.impl;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wrupple.vegetate.domain.VegetateToken;
import com.wrupple.vegetate.domain.exceptions.IntegrationLayerException;
import com.wrupple.vegetate.server.VegetateServiceLocator;
import com.wrupple.vegetate.server.integration.HttpServletRequestHandler;

public abstract class AbstractVegetateService implements VegetateServiceLocator {
	protected HttpServletRequestHandler requestHandler ;
	@Override
	public VegetateServiceLocator getServiceBranchForToken(VegetateToken token)
			throws IntegrationLayerException {
		String tokenName = token.getName();
		requestHandler= getHandlerForToken(tokenName);
		System.err.println("LETS SEE"+this.getClass().getCanonicalName()+" : "+ tokenName+" / "+requestHandler);
		VegetateServiceLocator serviceLocator = requestHandler.getChildServiceLocator();
		return serviceLocator;
	}

	@Override
	public void serve(HttpServletRequest req, HttpServletResponse resp, Map<String, String> pathParameters)
			throws IOException, IntegrationLayerException {
		requestHandler.handleRequest(req, resp, pathParameters);
	}

	protected abstract HttpServletRequestHandler getHandlerForToken(String tokenName);
}
